<template>
    <div class="manager">
        <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="50%"
             :before-close='closeForm'>
            <!-- 用户 -->
            <el-form ref="form" :rules='rules' :inline='true' :model="form" label-width="80px">
                <el-form-item label="姓名" prop="name">
                    <el-input placeholder="请输入姓名" v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="年龄">
                    <el-input placeholder="请输入年龄" v-model="form.age"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-select v-model="form.sex" placeholder="请选择性别">
                    <el-option label="男" :value="1"></el-option>
                    <el-option label="女" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期">
                    <el-col :span="22">
                    <el-date-picker value-format="yyyy-MM-DD" type="date" placeholder="选择日期" v-model="form.birth" style="width: 100%;"></el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input placeholder="请输入地址" v-model="form.addr"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="closeForm">取 消</el-button>
                <el-button type="primary" @click="submitForm('form')">确 定</el-button>
            </span>
        </el-dialog>    
        <div class="manager-search">
            <el-button type="primary" @click="clickAddNewItem">
                +新增
            </el-button>
            <el-form :inline='true' :model='userForm'>
                <el-form-item>
                    <el-input placeholder="请输入姓名" v-model="userForm.name"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="OnSearch">查询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="common-table">
            <!-- 数据表格 -->
            <el-table
                height="90%"
                :data="tableData"
                style="width: 100%">
                <el-table-column
                    prop="name"
                    label="姓名">
                </el-table-column>
                <el-table-column
                    prop="sex"
                    label="性別">
                    <template slot-scope="scope">
                        <span>{{scope.row.sex==1?'男':'女'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="age"
                    label="年齡">
                </el-table-column>
                <el-table-column
                    prop="birth"
                    label="出生日期">
                </el-table-column>
                <el-table-column
                    prop="addr"
                    label="地址">
                </el-table-column>
                <el-table-column
                prop="addr"
                    label="操作">
                    <template slot-scope="scope">
                        <el-button size='mini' @click="handleEdit(scope.row)">編輯</el-button>
                        <el-button type='danger' size='mini' @click="handleDelete(scope.row)">刪除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    layout="prev, pager, next"
                    :total="total"
                    @current-change="handlePage">
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { getUser,addUser, editUser, delUser } from "../api";

export default {
    name:'UserModel',
    data() {
        return {
            dialogVisible:false,
            form:{
                name:'',
                age:'',
                sex:'',
                birth:'',
                addr:''
            },
            rules:{
                name: [
                    { required: true, message: '请输入活动名称', trigger: 'blur' },
                    { max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
            },
            tableData:[],
            modelType:0,
            total:0, // 项目总数
            pageData:{
                page:1,
                limit:10
            },
            userForm:{
                name:''
            }
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                if(this.modelType === 0){
                    addUser(this.form).then(()=>{
                        this.getList()
                    })
                }else{
                    editUser(this.form).then(()=>{
                        this.getList()
                    })
                }

                this.$refs['form'].resetFields();
                this.dialogVisible=false;
            } else {
                console.log('error submit!!');
                return false;
            }
            })
        },
        closeForm(){
            this.$refs['form'].resetFields();
            this.dialogVisible=false;
        },
        handleEdit(item){
            this.modelType = 1
            this.dialogVisible=true
            // 深拷貝
            this.form=JSON.parse(JSON.stringify(item))
            alert('數據修改成功')
        },
        handleDelete(item){
            console.log('handleDelete',item)
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delUser({id:item.id}).then(()=>{
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getList()
                })
                
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
        },
        getList(){
            getUser({params:{...this.userForm, ...this.pageData}}).then((value)=>{
                const {data} = value
                this.tableData = data.list
                this.total = data.count || 0
                console.log('add------', data)
            })
        },
        clickAddNewItem(){
            this.dialogVisible=true
            this.modelType = 0
        },
        handlePage(val){
            console.log(val)
            this.pageData.page = val
            this.getList()
        },
        OnSearch(){
            this.getList()
        }
    },
    mounted() {
        this.getList()
    },
}
</script>


<style lang="less" scoped>
.manager{
    height: 90%;
    .manager-search{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .common-table{
        height: 100%;
        position: relative;
        .pagination{
            position: absolute;
            right: 20px;
            bottom: 10px;
        }
    }
}
</style>